package com.itheima.Service.CheckServicelmpl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.Service.CheckitemService;
import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.exception.checkitemException;
import com.itheima.mapper.CheckgroupCheckitemMapper;
import com.itheima.mapper.CheckitemMapper;
import com.itheima.pojo.CheckItem;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CheckitemServiceLmpl implements CheckitemService {
    @Autowired
    private CheckitemMapper checkitemMapper;
    @Autowired
    private CheckgroupCheckitemMapper checklistgroupCheckitemMapper;
    /**
     * 分页查询检查项
     * @param queryPageBean
     * @return
     */
    public PageResult list(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        CheckItem checkItem = new CheckItem();
        checkItem.setName(queryPageBean.getQueryString());
        BeanUtils.copyProperties(queryPageBean,checkItem);
        Page<CheckItem> page =checkitemMapper.list(checkItem);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 新增检查项
     * @param checkItem
     */
    public void listcheck(CheckItem checkItem) {
        checkitemMapper.listcheck(checkItem);
    }

    /**
     * 删除检查项
     * @param id
     */
    public void delectcheck(Integer id) {
        CheckItem readcheck = checkitemMapper.readcheck(id);
        List<Integer> ARR=checklistgroupCheckitemMapper.list(readcheck.getId());
        //存在关联的检查组,不行删除
        if (ARR!=null && ARR.size()>0){
            throw new checkitemException(MessageConstant.DELETE_CHECKITEM_FAIL);
        }
        checklistgroupCheckitemMapper.delect(readcheck.getId());
        checkitemMapper.delectcheck(id);
    }

    /**
     * 根据id查询检查项
     * @param id
     * @return
     */
    public CheckItem readcheck(Integer id) {
        return checkitemMapper.readcheck(id);
    }

    /**
     * 查询所有检查项
     * @return
     */
    public List<CheckItem> readlist() {
        return checkitemMapper.readlist();
    }

    /**
     * 根据检查组查询检查项数量
     * @param checkgroupId
     * @return
     */
    public List<Integer> listcheckgroup(Integer checkgroupId) {
        return checklistgroupCheckitemMapper.listcheckgroup(checkgroupId);
    }

    /**
     * 修改检查项
     * @param checkItem
     */
    public void updatecheck(CheckItem checkItem) {
        checkitemMapper.upadte(checkItem);
    }
}
